﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SGGWElevator
{
    public delegate void CallerClickedEventHandler(object sender, Floor floor, Direction direction);

    /// <summary>
    /// Interaction logic for ElevatorCaller.xaml
    /// Author: Konrad Żyliński
    /// </summary>
    public partial class ElevatorCaller : UserControl
    {
        #region Custom parameter : CallersFloor
        public static readonly DependencyProperty CallersFloorProperty = DependencyProperty.Register("Floor", typeof(Floor), typeof(ElevatorCaller));

        /// <summary>
        /// Contains callers floor.
        /// </summary>
        public Floor CallersFloor
        {
            get { return (SGGWElevator.Floor)GetValue(CallersFloorProperty); }
            set { SetValue(CallersFloorProperty, value); }
        }
        #endregion

        ///// <summary>
        ///// Is visible.
        ///// </summary>
        //public bool ArrowUpVisible
        //{
        //    get { return this.ArrowUp.Visibility.Equals(Visibility.Visible); }
        //    set { this.ArrowUp.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        //}

        ///// <summary>
        ///// Is visible.
        ///// </summary>
        //public bool ArrowDownVisible
        //{
        //    get { return this.ArrowUp.Visibility.Equals(Visibility.Visible); }
        //    set { this.ArrowUp.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        //}

        /// <summary>
        /// Is arrow visible.
        ///// </summary>
        //public bool IsArrowVisible(Control arrow)
        //{
        //    if (arrow == null)
        //        throw new ArgumentNullException();

        //    if (arrow.Visibility == Visibility.Visible)
        //        return true;
        //    else
        //        return false;
        //}

        //List<Control> _arrowsRowList = new List<Control>();

        ///// <summary>
        ///// Set arrows visibility. Raises event.
        ///// </summary>
        //public void SetArrowVisibility(Control arrow, bool visible)
        //{
        //    if (arrow == null)
        //        throw new ArgumentNullException();

        //    // Get actual visiblity of arrow
        //    bool isVisibleBefore = IsArrowVisible(arrow);

        //    arrow.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;

        //    ArrowDownRow.Height = new GridLength(0);
        //}

        /// <summary>
        /// Called when one of the callers is clicked.
        /// </summary>
        public event CallerClickedEventHandler CallerClickedEvent;

        public ElevatorCaller()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Caller click handler.
        /// </summary>
        /// <param name="sender">Caller.</param>
        /// <param name="args"></param>
        void OnButtonClick(object sender, RoutedEventArgs args)
        {
            Direction direction = Direction.Undifined;

            if (sender.Equals(ArrowUp))
            {
                direction = Direction.Up;
            }
            else if (sender.Equals(ArrowDown))
            {
                direction = Direction.Down;
            }
            else
                throw new ArgumentNullException("Sender is null!");

            // Update callers image
            ChangeCallersImage(sender as Button, direction, true);

            // Call event
            if (CallerClickedEvent != null)
                CallerClickedEvent(sender, CallersFloor, direction);
        }

        /// <summary>
        /// Changes callers image using dispatcher.
        /// </summary>
        /// <param name="caller"></param>
        /// <param name="direction"></param>
        private void ChangeCallersImage(Button caller, Direction direction, bool turnOn)
        {
            // Retrive buttons image
            var btnImage = caller.Content as Image;

            // Change image source
            string newImagePath = string.Empty;
            switch (direction)
            {
                case Direction.Up:
                    newImagePath = turnOn ? ImagePaths.CallerUpOn : ImagePaths.CallerUpOff;
                    break;
                case Direction.Down:
                    newImagePath = turnOn ? ImagePaths.CallerDownOn : ImagePaths.CallerDownOff;
                    break;
                default:
                    break;
            }


            btnImage.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                    delegate()
                    {
                        try
                        {
                            btnImage.Source = new BitmapImage(new Uri(newImagePath, UriKind.Relative));
                        }
                        catch (Exception ex) { throw ex; }
                    }
                )
            );
        }

        /// <summary>
        /// Turns off caller (both directions)
        /// </summary>
        /// <param name="caller"></param>
        /// <param name="turnOn"></param>
        public void TurnOffCaller()
        {
            ChangeCallersImage(ArrowDown, Direction.Down, false);
            ChangeCallersImage(ArrowUp, Direction.Up, false);
        }

        /// <summary>
        /// Paths to images.
        /// </summary>
        class ImagePaths
        {
            public readonly static string CallerDownOff = @"/Images/ElevatorCallers/CallerDownOff.png";
            public readonly static string CallerDownOn = @"/Images/ElevatorCallers/CallerDownOn.png";
            public readonly static string CallerUpOff = @"/Images/ElevatorCallers/CallerUpOff.png";
            public readonly static string CallerUpOn = @"/Images/ElevatorCallers/CallerUpOn.png";
        }
    }
}
